package com.wzh.eduservice.controller;


import com.wzh.commonutils.R;
import com.wzh.eduservice.client.VodClient;
import com.wzh.eduservice.entity.EduVideo;
import com.wzh.eduservice.service.EduVideoService;
import com.wzh.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    // 删除小节时候，同时把里面视频删除
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {
        //根据小节id获得视频id
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            //远程调用删除视频
            R r = vodClient.removeAlyVideo(videoSourceId);
            if(r.getCode() == 20001){
                throw new GuliException(20001, "删除视频失败，熔断器...");
            }
        }
        //删除小节
        vodClient.removeAlyVideo(id);
        return R.ok();
    }

    //修改小节 TODO

}
